package sudoku.master;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class BoardView extends View {
	
	private int viewSideLength;
	private float boardSideLength;
	private float blockLength;
	private float cellLength;
	private Paint outerPaint;
	private Paint blockPaint;
	private Paint cellPaint;

	public BoardView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public BoardView(Context context, AttributeSet attrs){
		super(context, attrs);
	}

	private void setCellPaint() {
		cellPaint = new Paint();
		cellPaint.setColor(Color.BLACK);
		cellPaint.setStyle(Style.STROKE);
		cellPaint.setStrokeWidth(1);		
	}

	private void drawCell(Canvas canvas) {
		// Draw 6 vertical lines and 6 horizontal lines
		for (int i=1; i<9; i++){
			if (i % 3 != 0){
				canvas.drawLine(i*cellLength, 0, i*cellLength, boardSideLength, cellPaint);
				canvas.drawLine(0, i*cellLength, boardSideLength, i*cellLength, cellPaint);
			}
		}
	}

	private void setBlockPaint() {
		blockPaint = new Paint();
		blockPaint.setColor(Color.BLACK);
		blockPaint.setStyle(Style.STROKE);
		blockPaint.setStrokeWidth(2);
	}

	private void drawBlock(Canvas canvas) {
		blockLength = boardSideLength/3;
		// Draw 2 vertical lines and 2 horizontal lines
		for (int i=1; i<3; i++){
			canvas.drawLine(i*blockLength, 0, i*blockLength, boardSideLength, blockPaint);
			canvas.drawLine(0, i*blockLength, boardSideLength, i*blockLength, blockPaint);
		}
	}

	private void setOuterPaint() {
		outerPaint = new Paint();
		outerPaint.setColor(Color.WHITE);
		outerPaint.setStyle(Style.FILL);
	}

	private void drawOuter(Canvas canvas) {
		canvas.drawRect(0, 0, getWidth(), getHeight(), outerPaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
				
		if (widthSize < heightSize){
			viewSideLength = widthSize;
		} else {
			viewSideLength = heightSize;
		}
		setMeasuredDimension(viewSideLength, viewSideLength);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		boardSideLength = viewSideLength;
		cellLength = boardSideLength/9;
		setOuterPaint();
		drawOuter(canvas);
		setBlockPaint();
		drawBlock(canvas);
		setCellPaint();
		drawCell(canvas);
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(state);
	}

	@Override
	protected Parcelable onSaveInstanceState() {
		// TODO Auto-generated method stub
		return super.onSaveInstanceState();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setGameSpec(GameInfo gameInfo) {
		// TODO Auto-generated method stub
		
	}
}
